package com.aiyo.service.impl;

import com.aiyo.entity.BaseDictionary;
import com.aiyo.service.BaseDictionaryService;
import com.aiyo.mapper.BaseDictionaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author ylc
 * @since 2020-04-13
 */
@Service
@Slf4j
public class BaseDictionaryServiceImpl implements BaseDictionaryService {

    @Autowired
    private BaseDictionaryMapper baseDictionaryMapper;

    @Override
    public List<BaseDictionary> list() {
        BaseDictionary baseDictionary = new BaseDictionary();
        return baseDictionaryMapper.select(baseDictionary);
    }

    @Override
    public JsonData get(Long id) {
        if (id == null) {
            return JsonData.buildError("id不能为空");
        }
        BaseDictionary baseDictionary = baseDictionaryMapper.selectByPrimaryKey(id);
        return JsonData.buildSuccess(baseDictionary);
    }

    @Override
    public JsonData add(BaseDictionary baseDictionary) {
        if (baseDictionary == null) {
            return JsonData.buildError("添加失败");
        }
        int i = baseDictionaryMapper.insertSelective(baseDictionary);
        return i == 0 ? JsonData.buildError("添加失败") : JsonData.buildSuccess();
    }

    @Override
    public JsonData update(BaseDictionary baseDictionary) {
        if (baseDictionary == null || baseDictionary.getId() == null) {
            return JsonData.buildError("ID不能为空");
        }
        int i = baseDictionaryMapper.updateByPrimaryKeySelective(baseDictionary);
        return i == 0 ? JsonData.buildError("修改失败") : JsonData.buildSuccess();
    }

    @Override
    public JsonData delete(Long id) {
        if (id == null) {
            return JsonData.buildError("id不能为空");
        }
        BaseDictionary baseDictionary = baseDictionaryMapper.selectByPrimaryKey(id);
        if (baseDictionary != null) {
            baseDictionary.setLogicalState(2);
            int i = baseDictionaryMapper.updateByPrimaryKeySelective(baseDictionary);
            return i == 0 ? JsonData.buildError("删除失败") : JsonData.buildSuccess();
        }
        return JsonData.buildError("删除失败");
    }

}
